//
// Created by yonghu0 on 25-10-19.
//

#include "Control.h"
#include "UserList.h"
#include "ProblemList.h"
#include "Tag.h"
#include "AnnouncementList.h"
#include "DiscussList.h"
#include "SolutionList.h"
#include "CommentList.h"
#include "StatusRecordList.h"
#include "Judger.h"

// 无权限
static Json::Value NoPermission;

Control::Control()
{
    // 初始化题目标签
    Tag::GetInstance()->InitProblemTags();

    UserList::GetInstance()->InitUserAuthority();

    // 初始化返回变量
    NoPermission["Result"] = "401";
    NoPermission["Reason"] = "无权限";
}

Control::~Control()
{
}

Json::Value Control::RegisterUser(Json::Value registerjson)
{
    return UserList::GetInstance()->RegisterUser(registerjson);
}

Json::Value Control::LoginUser(Json::Value loginjson)
{
    return UserList::GetInstance()->LoginUser(loginjson);
}

Json::Value Control::SelectUserInfo(Json::Value selectjson)
{
    return UserList::GetInstance()->SelectUserInfo(selectjson);
}

Json::Value Control::UpdateUser(Json::Value updatejson)
{
    // 如果不是本人或者管理员 无权修改
    if (!UserList::GetInstance()->IsAuthor(updatejson))
        return NoPermission;

    return UserList::GetInstance()->UpdateUser(updatejson);
}

Json::Value Control::GetUserUpdateInfo(Json::Value getjson)
{
    // 如果不是本人或者管理员 无权修改
    if (!UserList::GetInstance()->IsAuthor(getjson))
        return NoPermission;

    return UserList::GetInstance()->GetUserUpdateInfo(getjson);
}

Json::Value Control::GetUserlistInfo(Json::Value getjson)
{
    // 如果不是管理员，无权查询
    if (!UserList::GetInstance()->IsAdministrator(getjson))
        return NoPermission;

    return UserList::GetInstance()->GetUserlistInfo(getjson);
}

Json::Value Control::DeleteUser(Json::Value deletejson)
{
    // 如果不是管理员，无权查询
    if (!UserList::GetInstance()->IsAdministrator(deletejson))
        return NoPermission;

    return UserList::GetInstance()->DeleteUser(deletejson);
}

Json::Value Control::RankUser(Json::Value rankjson)
{
    return UserList::GetInstance()->RankUser(rankjson);
}

Json::Value Control::TokenLoginUser(Json::Value loginjson)
{
    return UserList::GetInstance()->TokenLoginUser(loginjson);
}

Json::Value Control::SelectProblem(Json::Value queryjson)
{
    return ProblemList::GetInstance()->SelectProblem(queryjson);
}

Json::Value Control::SelectProblemList(Json::Value queryjson)
{
    return ProblemList::GetInstance()->SelectProblemList(queryjson);
}

Json::Value Control::SelectProblemListByAdmin(Json::Value queryjson)
{
    // 如果不是管理员，无权查看
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return ProblemList::GetInstance()->SelectProblemListByAdmin(queryjson);
}

Json::Value Control::SelectProblemInfoByAdmin(Json::Value queryjson)
{
    // 如果不是管理员，无权查看
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return ProblemList::GetInstance()->SelectProblemInfoByAdmin(queryjson);
}

Json::Value Control::EditProblem(Json::Value queryjson)
{
    // 如果不是管理员，无权查看
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    Json::Value resjson;
    if (queryjson["EditType"].asString() == "Insert")
    {
        resjson = ProblemList::GetInstance()->InsertProblem(queryjson);
    }
    else if (queryjson["EditType"].asString() == "Update")
    {
        resjson = ProblemList::GetInstance()->UpdateProblem(queryjson);
    }
    Tag::GetInstance()->InitProblemTags();
    return resjson;
}

Json::Value Control::DeleteProblem(Json::Value queryjson)
{
    // 如果不是管理员，无权查看
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    Json::Value resjson = ProblemList::GetInstance()->DeleteProblem(queryjson);
    Tag::GetInstance()->InitProblemTags();
    return resjson;
}

Json::Value Control::GetTags(Json::Value queryjson)
{
    if (queryjson["TagType"].asString() == "Problem")
        return Tag::GetInstance()->GetProblemTags();
    return Json::Value{};
}

Json::Value Control::SelectAnnouncementList(Json::Value queryjson)
{
    return AnnouncementList::GetInstance()->SelectAnnouncementList(queryjson);
}

Json::Value Control::SelectAnnouncementListByAdmin(Json::Value queryjson)
{
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return AnnouncementList::GetInstance()->SelectAnnouncementListByAdmin(queryjson);
}

Json::Value Control::SelectAnnouncement(Json::Value queryjson)
{
    return AnnouncementList::GetInstance()->SelectAnnouncement(queryjson);
}

Json::Value Control::SelectAnnouncementByEdit(Json::Value queryjson)
{
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return AnnouncementList::GetInstance()->SelectAnnouncementByEdit(queryjson);
}

Json::Value Control::InsertAnnouncement(Json::Value queryjson)
{
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return AnnouncementList::GetInstance()->InsertAnnouncement(queryjson);
}

Json::Value Control::UpdateAnnouncement(Json::Value queryjson)
{
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return AnnouncementList::GetInstance()->UpdateAnnouncement(queryjson);
}

Json::Value Control::DeleteAnnouncement(Json::Value queryjson)
{
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    Json::Value resjson = AnnouncementList::GetInstance()->DeleteAnnouncement(queryjson);

    //公告删除时，要删除下面的评论
    if (resjson["Result"].asString() == "Success")
    {
        Json::Value json;
        json["ArticleId"] = queryjson["SolutionId"];
        CommentList::GetInstance()->DeleteArticleComment(json);
    }
    return resjson;
}

Json::Value Control::SelectDiscussList(Json::Value queryjson)
{
    return DiscussList::GetInstance()->SelectDiscussList(queryjson);
}

Json::Value Control::SelectDiscussListByAdmin(Json::Value queryjson)
{
    // 如果不是管理员，无权限
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return DiscussList::GetInstance()->SelectDiscussListByAdmin(queryjson);
}

Json::Value Control::SelectDiscuss(Json::Value queryjson)
{
    return DiscussList::GetInstance()->SelectDiscuss(queryjson);
}

Json::Value Control::SelectDiscussByEdit(Json::Value queryjson)
{
    // 如果不是用户，无权限
    if (!UserList::GetInstance()->IsOrdinaryUser(queryjson))
        return NoPermission;

    return DiscussList::GetInstance()->SelectDiscussByEdit(queryjson);
}

Json::Value Control::InsertDiscuss(Json::Value queryjson)
{
    // 如果不是用户，无权限
    if (!UserList::GetInstance()->IsOrdinaryUser(queryjson))
        return NoPermission;

    return DiscussList::GetInstance()->InsertDiscuss(queryjson);
}

Json::Value Control::UpdateDiscuss(Json::Value queryjson)
{
    // 如果不是作者或者管理员，无权限
    if (!UserList::GetInstance()->IsAuthor(queryjson))
        return NoPermission;

    return DiscussList::GetInstance()->UpdateDiscuss(queryjson);
}

Json::Value Control::DeleteDiscuss(Json::Value queryjson)
{
    // 如果不是作者或者管理员，无权限
    if (!UserList::GetInstance()->IsAuthor(queryjson))
        return NoPermission;

    Json::Value resjson = DiscussList::GetInstance()->DeleteDiscuss(queryjson);

    //删除讨论时，要删除下面的评论
    if (resjson["Result"].asString() == "Success")
    {
        Json::Value json;
        json["ArticleId"] = queryjson["SolutionId"];
        CommentList::GetInstance()->DeleteArticleComment(json);
    }
    return resjson;
}

Json::Value Control::SelectSolutionList(Json::Value &queryjson)
{
    return SolutionList::GetInstance()->SelectSolutionList(queryjson);
}

Json::Value Control::SelectSolutionListByAdmin(Json::Value &queryjson)
{
    // 如果不是管理员，无权限
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return SolutionList::GetInstance()->SelectSolutionListByAdmin(queryjson);
}

Json::Value Control::SelectSolution(Json::Value &queryjson)
{
    return SolutionList::GetInstance()->SelectSolution(queryjson);
}

Json::Value Control::SelectSolutionByEdit(Json::Value &queryjson)
{
    // 如果不是用户，无权限
    if (!UserList::GetInstance()->IsOrdinaryUser(queryjson))
        return NoPermission;

    return SolutionList::GetInstance()->SelectSolutionByEdit(queryjson);
}

Json::Value Control::InsertSolution(Json::Value &queryjson)
{
    // 如果不是用户，无权限
    if (!UserList::GetInstance()->IsOrdinaryUser(queryjson))
        return NoPermission;

    return SolutionList::GetInstance()->InsertSolution(queryjson);
}

Json::Value Control::UpdateSolution(Json::Value &queryjson)
{
    // 如果不是作者或者管理员，无权限
    if (!UserList::GetInstance()->IsAuthor(queryjson))
        return NoPermission;

    return SolutionList::GetInstance()->UpdateSolution(queryjson);
}

Json::Value Control::DeleteSolution(Json::Value &queryjson)
{
    // 如果不是作者或者管理员，无权限
    if (!UserList::GetInstance()->IsAuthor(queryjson))
        return NoPermission;

    Json::Value resjson = SolutionList::GetInstance()->DeleteSolution(queryjson);

    //删除题解时，要删除下面的评论
    if (resjson["Result"].asString() == "Success")
    {
        Json::Value json;
        json["ArticleId"] = queryjson["SolutionId"];
        CommentList::GetInstance()->DeleteArticleComment(json);
    }
    return resjson;
}

Json::Value Control::SelectCommentListByAdmin(Json::Value &queryjson)
{
    // 如果不是管理员，无权查看
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    return CommentList::GetInstance()->SelectCommentListByAdmin(queryjson);
}

Json::Value Control::GetComment(Json::Value &queryjson)
{
    if (queryjson["Type"].asString() == "Father")
    {
        return CommentList::GetInstance()->GetFatherComment(queryjson);
    }
    else
    {
        return CommentList::GetInstance()->GetSonComment(queryjson);
    }
}

Json::Value Control::InsertComment(Json::Value &queryjson)
{
    // 如果不是用户，无权限
    if (!UserList::GetInstance()->IsOrdinaryUser(queryjson))
        return NoPermission;

    Json::Value updatejson;
    updatejson["ArticleId"] = queryjson["ArticleId"].asString();
    updatejson["Num"] = 1;

    //更新评论数
    if (queryjson["ArticleType"] == "Announcement")
    {
        AnnouncementList::GetInstance()->UpdateAnnouncementComments(updatejson);
    }
    else if (queryjson["ArticleType"] == "Discuss")
    {
        DiscussList::GetInstance()->UpdateDiscussComments(updatejson);
    }
    else if (queryjson["ArticleType"] == "Solution")
    {
        SolutionList::GetInstance()->UpdateSolutionComments(updatejson);
    }
    if (queryjson["Type"] == "Father")
    {
        return CommentList::GetInstance()->InsertFatherComment(queryjson);
    }
    else
    {
        return CommentList::GetInstance()->InsertSonComment(queryjson);
    }
}

Json::Value Control::DeleteComment(Json::Value &queryjson)
{
    // 如果不是用户，没有权限删除
    if (!UserList::GetInstance()->IsAdministrator(queryjson))
        return NoPermission;

    Json::Value resjson;

    //删除父评论
    Json::Value json = CommentList::GetInstance()->DeleteFatherComment(queryjson);

    //如果失败，删除子评论
    if (json["Result"] == "Fail")
    {
        json = CommentList::GetInstance()->DeleteSonComment(queryjson);
    }

    //如果都失败，返回
    if (json["Result"] == "Fail")
    {
        resjson["Result"] = "Fail";
        resjson["Reason"] = "数据库未查询到数据！";
        return resjson;
    }

    //根据ArticleId更新评论数
    Json::Value articlejson;
    articlejson["ArticleId"] = queryjson["ArticleId"];
    articlejson["Num"] = (-1) * json["DeleteNum"].asInt();

    std::string articletype = json["ArticleType"].asString();
    if (articletype == "Discuss")
    {
        DiscussList::GetInstance()->UpdateDiscussComments(articlejson);
    }
    else if (articletype == "Solution")
    {
        SolutionList::GetInstance()->UpdateSolutionComments(articlejson);
    }
    else if (articletype == "Announcement")
    {
        AnnouncementList::GetInstance()->UpdateAnnouncementComments(articlejson);
    }

    resjson["Result"] = "Success";
    return resjson;
}

Json::Value Control::GetJudgeCode(Json::Value &queryjson)
{
    // 如果不是用户，无权操作
    if (!UserList::GetInstance()->IsOrdinaryUser(queryjson))
        return NoPermission;

    Json::Value resjson;

    Json::Value insertjson;
    insertjson["ProblemId"] = queryjson["ProblemId"];
    insertjson["UserId"] = queryjson["UserId"];
    insertjson["UserNickName"] = queryjson["UserNickName"];
    insertjson["ProblemTitle"] = queryjson["ProblemTitle"];
    insertjson["Language"] = queryjson["Language"];
    insertjson["Code"] = queryjson["Code"];
    std::string submitid = StatusRecordList::GetInstance()->InsertStatusRecord(insertjson);

    if (submitid == "0")
    {
        resjson["Result"] = "Fail";
        resjson["Reason"] = "系统出错！";
        return resjson;
    }

    Json::Value runjson;
    runjson["SubmitId"] = submitid;
    runjson["ProblemId"] = queryjson["ProblemId"];
    runjson["JudgeNum"] = queryjson["JudgeNum"];
    runjson["Code"] = queryjson["Code"];
    runjson["Language"] = queryjson["Language"];
    runjson["TimeLimit"] = queryjson["TimeLimit"];
    runjson["MemoryLimit"] = queryjson["MemoryLimit"];
    Judger judger;
    Json::Value json = judger.Run(runjson);

    //更新statusrecord表
    StatusRecordList::GetInstance()->UpdateStatusRecord(json);

    //更新problem表
    Json::Value updatejson;
    updatejson["ProblemId"] = queryjson["ProblemId"];
    updatejson["Status"] = json["Status"];
    ProblemList::GetInstance()->UpdateProblemStatusNum(updatejson);

    //更新user表
    updatejson["UserId"] = queryjson["UserId"];
    if (UserList::GetInstance()->UpdateUserProblemInfo(updatejson))
        resjson["IsFirstAC"] = true;
    else
        resjson["IsFirstAC"] = false;

    resjson["Result"] = "Success";
    resjson["Status"] = json["Status"];
    resjson["ComplierInfo"] = json["ComplierInfo"];
    return resjson;
}

Json::Value Control::SelectStatusRecordList(Json::Value &queryjson)
{
    return StatusRecordList::GetInstance()->SelectStatusRecordList(queryjson);
}

Json::Value Control::SelectStatusRecord(Json::Value &queryjson)
{
    return StatusRecordList::GetInstance()->SelectStatusRecord(queryjson);
}
